﻿using System;
using System.Data;
using System.Data.SqlClient;
using consultasSql.consultasVarias;

/// <summary>
/// Descripción breve de CComprobarExisteRegistro
/// </summary>
public class CExisteRegistro : CDarDataSet
{
    public bool existeRegistro(string tabla, string campo, string dato)
	{
        bool existe = false;

        if (!String.IsNullOrEmpty(tabla) && !String.IsNullOrEmpty(campo) && !String.IsNullOrEmpty(dato))
        {
            existe = comprobar(tabla, campo, dato);
        }

        return existe;
	}

    private bool comprobar(string tabla, string campo, string dato)
    {
        SqlCommand command = new SqlCommand();
                
        command.CommandType = CommandType.Text;
        command.CommandText = "IF EXISTS (SELECT 1 FROM " + tabla + " WHERE " + campo + " = @dato) SELECT 1 ELSE SELECT 0";

        command.Parameters.Clear();

        command.Parameters.AddWithValue("@dato", dato.Trim());

        
        DataTable dt = darDataTable(command);

        if (dt != null)
        {
            try
            {
                if (dt.Rows[0][0] == null)
                    return false;
                else
                    return Convert.ToBoolean(dt.Rows[0][0]);
            }
            catch { }

        }

        return false;

    }

}